package thread;

/**
 * 测试多线程并发安全问题
 */
public class SyncDemo {
    private static boolean success = false;
    private static boolean success2 = false;

    public static void main(String[] args) {
        Bank bank = new Bank();
        int sum = 0;
        while (true) {
            sum++;
            Thread t1= new Thread("三儿"){
                public void run(){success = bank.getMoney(20000);}
            };
            Thread t2 = new Thread("李四"){
                public void run(){
                    success2 = bank.getMoney(20000);
                }
            };

            t1.start();
            t2.start();

            try {
                //让主线程阻塞5毫秒，目的是等待t1,t2线程执行完毕
                Thread.sleep(5);
            } catch (InterruptedException e) {
            }

            if(success&&success2){
                System.out.println("两个人都群款成功！！出现了问题!");
                System.out.println("总共尝试了:" + sum +"次");
                break;
            }else{
                bank.saveAccount(20000);//重新存上
            }
        }
    }
}
